<?php
/**
 * Created by PhpStorm.
 * User: jc234839
 * Date: 20/04/14
 * Time: 11:02 AM
 */
require 'init.php';

if (empty($_POST) === false) {
    $email = trim($_POST['email']);
    $password = trim($_POST['password']);

    if (empty($email) === true || empty($password) === true) {
        $message[] = 'All fields are required.';
    } else if ($user->userExists($email) === false) {
        $message[] = 'Incorrect Email Address or Password';
    } else {
        $validCredentials = $user->logIn($email, $password);

        if (!$validCredentials) {
            $message[] = 'Incorrect Email Address or Password';
        } else {
            session_regenerate_id(true);

            $_SESSION['id'] = $validCredentials;
            $user = $user->getUserInfo($_SESSION['id']);
            $isAdmin = $user['IsAdmin'];

            if ($isAdmin) {
                header('Location: admin.php');
            } else {
                if (isset($_SESSION['previousPage'])) {
                    header('Location: ' . $_SESSION['previousPage']);
                } else {
                    header('Location: index.php');
                }
            }
            exit();
        }
    }
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link href="css/main.css" rel="stylesheet" type="text/css">
    <script src="javascripts/validateForm.js"></script>
    <title>Log In</title>
</head>
<body>
<div id="container">
    <div id="leftCogSplash"><img src="images/cogLeft.png" alt="Left Steampunk Cog"/></div>
    <div id="rightCogSplash"><img src="images/cogRight.png" alt="Right Steampunk Cog"/></div>
    <?php include "navbar.php"; ?>
    <div id="content">
        <h2>Log In</h2>

        <form action="login.php" method="post" name="logInForm" id="logInForm">
            <label for="email">Email:</label>
            <input type="text" name="email" id="email" size="35"
                   value="<?php if (isset($_POST['email'])) echo htmlentities($_POST['email']); ?>"/>
            <!--                   onblur="validateEmail();"/>-->
            <br/>
            <label for="password">Password:</label>
            <input type="password" name="password" id="password" size="20"/> <!--onblur="validatePassword();"-->
            <br/>

            <div id="submission">
                <input type="submit" value="Log In" id="logInButton"/>
                <a <?php if ($pageName == "register.php") {
                    echo 'class="current"';
                } ?> href="register.php">Not a member?</a>
            </div>
            <div id="statusBar">
                <?php
                if (empty($message) === false) {
                    echo "<script type='text/javascript'>document.getElementById('statusBar').style.display = 'block';</script>";
                    echo '<p>' . implode('<p></p>', $message) . '</p>';
                }
                ?>
            </div>
        </form>
    </div>
    <?php require 'footer.php' ?>
</div>
</body>
</html>
